package com.cnswhy.cloud.server.comsumer.consumer.iot;

import com.cnswhy.cloud.common.core.exception.BizException;
import com.cnswhy.cloud.common.core.mybatis.conditions.Wraps;
import com.cnswhy.cloud.common.redis.annotation.RedisLock;
import com.cnswhy.cloud.common.redis.model.LockTimeoutStrategy;
import com.cnswhy.cloud.server.common.entity.enumeration.IotType;
import com.cnswhy.cloud.server.common.entity.mq.CreateTerminalMQ;
import com.cnswhy.cloud.server.comsumer.event.IotTerminalCreateEvent;
import com.cnswhy.cloud.server.core.entity.IotChargingLineInfo;
import com.cnswhy.cloud.server.core.entity.IotTerminalApplyInfo;
import com.cnswhy.cloud.server.core.entity.IotTerminalIndexConfig;
import com.cnswhy.cloud.server.core.entity.IotTerminalInfo;
import com.cnswhy.cloud.server.core.service.IotChargingLineInfoService;
import com.cnswhy.cloud.server.core.service.IotTerminalApplyInfoService;
import com.cnswhy.cloud.server.core.service.IotTerminalIndexConfigService;
import com.cnswhy.cloud.server.core.service.IotTerminalInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class IotTerminalCreateConsumer implements IotTerminalCreateEvent {
    @Autowired
    IotTerminalApplyInfoService iotTerminalApplyInfoService;
    @Autowired
    IotTerminalInfoService iotTerminalInfoService;
    @Autowired
    IotTerminalIndexConfigService iotTerminalIndexConfigService;
    @Autowired
    IotChargingLineInfoService iotChargingLineInfoService;
    @Override
    public void create(CreateTerminalMQ createTerminalMQ) throws BizException {
        IotTerminalApplyInfo iotTerminalApplyInfo = iotTerminalApplyInfoService.getById(createTerminalMQ.getIotTerminalApplyInfoId());
        if(iotTerminalApplyInfo == null){
            log.error("终端量产申请未找到");
        }
        IotTerminalIndexConfig iotTerminalIndexConfig = iotTerminalIndexConfigService.getOne(Wraps.<IotTerminalIndexConfig>lbQ().eq(IotTerminalIndexConfig::getIotType,iotTerminalApplyInfo.getIotType()));
        if(iotTerminalIndexConfig == null){
            iotTerminalIndexConfig = new IotTerminalIndexConfig();
            iotTerminalIndexConfig.setIotType(iotTerminalApplyInfo.getIotType());
            iotTerminalIndexConfig.setCurrIndex(0L);
            iotTerminalIndexConfigService.save(iotTerminalIndexConfig);
        }
        //开始量产
        iotTerminalApplyInfo.setStartIndex(getIndex(iotTerminalApplyInfo.getIotType(),iotTerminalApplyInfo.getApplySum()));
        iotTerminalApplyInfo.setEndIndex(iotTerminalApplyInfo.getStartIndex() + iotTerminalApplyInfo.getApplySum() - 1);
        for(Long index = iotTerminalApplyInfo.getStartIndex() ; index <= iotTerminalApplyInfo.getEndIndex() ; index ++){
            IotTerminalInfo iotTerminalInfo = new IotTerminalInfo();
            iotTerminalInfo.setIotType(iotTerminalApplyInfo.getIotType());
            iotTerminalInfo.setStatus(0);
            iotTerminalInfo.setActiveStatus(0);
            iotTerminalInfo.setBatchOrderId(iotTerminalApplyInfo.getBatchOrderId());
            iotTerminalInfo.setTerminalId(String.format("2021%010d", index));
            //https://iot.weilaihehchuang.cn/iot/qrcode/iotcl/20010082
            if(IotType.IOT_CHARG_LINE.eq(iotTerminalApplyInfo.getIotType())){
                iotTerminalInfo.setQrCode("https://iot.weilaihehchuang.cn/iot/qrcode/iotcl/" + iotTerminalInfo.getTerminalId());
                IotChargingLineInfo iotChargingLineInfo = new IotChargingLineInfo();
                iotChargingLineInfo.setTerminalId(iotTerminalInfo.getTerminalId());
                iotChargingLineInfo.setStatus(0);
                iotChargingLineInfo.setAIndex(0);
                iotChargingLineInfo.setBIndex(0);
                iotChargingLineInfo.setCIndex(0);
                iotChargingLineInfoService.save(iotChargingLineInfo);
            }

            iotTerminalInfoService.save(iotTerminalInfo);
        }
        iotTerminalApplyInfo.setStatus(2);
        iotTerminalApplyInfoService.updateById(iotTerminalApplyInfo);

    }
    @RedisLock(name = "IotTerminalCreateConsumer.getIndex", waitTime = 10,leaseTime = 60,keys = {"#iotType"},lockTimeoutStrategy = LockTimeoutStrategy.FAIL_FAST)
    public Long getIndex(IotType iotType,Long applySum){
        IotTerminalIndexConfig iotTerminalIndexConfig = iotTerminalIndexConfigService.getOne(Wraps.<IotTerminalIndexConfig>lbQ().eq(IotTerminalIndexConfig::getIotType,iotType));
        if(iotTerminalIndexConfig == null){
            iotTerminalIndexConfig = new IotTerminalIndexConfig();
            iotTerminalIndexConfig.setIotType(iotType);
            iotTerminalIndexConfig.setCurrIndex(0L);
            iotTerminalIndexConfigService.save(iotTerminalIndexConfig);
        }
        Long currIndex = iotTerminalIndexConfig.getCurrIndex();
        iotTerminalIndexConfig.setCurrIndex(iotTerminalIndexConfig.getCurrIndex()+ applySum);
        iotTerminalIndexConfigService.updateById(iotTerminalIndexConfig);
        return currIndex;
    }
}
